package com.ymatou.shop.account;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.ymatou.shop.Constants;
import com.ymatou.shop.cache.CacheFiles;
import com.ymatou.shop.cache.CacheManager;
import com.ymatou.shop.cache.RequestInfo;
import com.ymatou.shop.location.Location;
import com.ymatou.shop.model.LoginResult;
import com.ymatou.shop.util.StringUtil;

public final class Account implements SharedPreferences.OnSharedPreferenceChangeListener {

	private static final String PREF_TOKEN = "token";
	private static final String PREF_USER_ID = "user_id";
	private static final String PREF_NICKNAME = "nickname";
	private static final String PREF_USER_TYPE = "user_type";
	private static final String PREF_USER_PERMISSION = "user_permission";
	private static final String PREF_USER_ICON = "user_icon";
	private static final int TYPE_SELLER = 1;
	private static final int TYPE_BUYER = 0;
	//	private static final String PREF_SINA_ACCESS_TOKEN = "pref_sina_access_token";
	//	private static final String PREF_SINA_EXPIRES_IN = "pref_sina_expires_in";
	//	private static final String PREF_RENREN_ACCESS_TOKEN = "pref_renren_access_token";
	//	private static final String PREF_RENREN_EXPIRES_IN = "pref_renren_expires_in";
	//	private static final String PREF_TENCENT_ACCESS_TOKEN = "pref_tencent_access_token";
	//	private static final String PREF_TENCENT_OPEN_ID = "pref_tencent_open_id";
	//	private static final String PREF_TENCENT_EXPIRES_IN = "pref_tencent_expires_in";

	private static final String WHERE_CLEAR_USER_DATA = CacheFiles.AUTHLEVEL + ">=" + RequestInfo.AUTHLEVEL_USER;

	private static Account sInstance;
	private final Context mContext;
	
	private final SharedPreferences mPreferences;

	private String mToken;
	private String mUserId;
	private String nickName;
	private String icon;
	private Location location;

	private int userType;
	private boolean isPermission;

	private Account(Context context) {
		mContext = context;
		mPreferences = context.getSharedPreferences(Constants.FILE_PREFERENCE_ACCOUNT, Context.MODE_PRIVATE);
		sync();
	}

	/**
	 * 
	 * @return true 卖家 false 买家
	 */
	public boolean isSeller() {
		return userType == TYPE_SELLER;
	}

	public static Account getInstance(Context context) {
		synchronized (Account.class) {
			if (sInstance == null) {
				sInstance = new Account(context.getApplicationContext());
			}
		}

		return sInstance;
	}

	public Location getLocation() {
  	return location;
  }

	public void setLocation(Location location) {
  	this.location = location;
  }

	public String getUserId() {
		return mUserId;
	}

	public String getIcon() {
  	return icon;
  }
	
	public String getToken() {
		return mToken;
	}
	
	public String getNickName() {
  	return nickName;
  }

	public int getUserType() {
  	return userType;
  }

	public boolean isPermission() {
  	return isPermission;
  }

	/*
	 * 清除用户缓存数据
	 */
	private void cleanUserCache() {
		CacheManager.getInstance(mContext).cleanCache(WHERE_CLEAR_USER_DATA, null);
	}

	public boolean isLogin() {
		return !TextUtils.isEmpty(mToken) && isPermission;
	}

	public void login(String userId, String token) {
		cleanUserCache();

		if (StringUtil.isNotEmpty(userId) && StringUtil.isNotEmpty(token)) {
			SharedPreferences.Editor editor = mPreferences.edit();
			editor.putString(PREF_TOKEN, token);
			editor.putString(PREF_USER_ID, userId);
			editor.commit();
		}
	}

	public void saveUserinfo(LoginResult loginResul) {
		userType = loginResul.mType;
		nickName = loginResul.mNickname;
		icon = loginResul.mIcon;
		if (StringUtil.isNotEmpty(loginResul.mNickname)) {
			SharedPreferences.Editor editor = mPreferences.edit();
			editor.putString(PREF_NICKNAME, nickName);
			editor.putString(PREF_USER_ICON, icon);
			editor.putInt(PREF_USER_TYPE, userType);
			editor.commit();
		}
	}

	public void savePermission() {
		isPermission = true;
		SharedPreferences.Editor editor = mPreferences.edit();
		editor.putBoolean(PREF_USER_PERMISSION, isPermission);
		editor.commit();
	}

	/**
	 * 退出登录
	 */
	public void logout() {
		cleanUserCache();

		SharedPreferences.Editor editor = mPreferences.edit();
		editor.putString(PREF_TOKEN, null);
		editor.putString(PREF_USER_ID, null);
		editor.clear();
		editor.commit();
	}

	public void sync() {
		mPreferences.registerOnSharedPreferenceChangeListener(this);

		mToken = mPreferences.getString(PREF_TOKEN, null);
		mUserId = mPreferences.getString(PREF_USER_ID, null);
		nickName = mPreferences.getString(PREF_NICKNAME, "");
		userType = mPreferences.getInt(PREF_USER_TYPE, TYPE_BUYER);
		isPermission = mPreferences.getBoolean(PREF_USER_PERMISSION, false);
		icon = mPreferences.getString(PREF_USER_ICON, "");
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
		if (StringUtil.isEmpty(key)) {
			return;
		}

		if (key.equals(PREF_TOKEN)) {
			mToken = mPreferences.getString(PREF_TOKEN, null);
		} else if (key.equals(PREF_USER_ID)) {
			mUserId = mPreferences.getString(PREF_USER_ID, null);
		}
	}
	//
	//	public String getSinaAccessToken() {
	//		return mPreferences.getString(PREF_SINA_ACCESS_TOKEN, "");
	//
	//	}
	//
	//	public String getSinaExpiresIn() {
	//		String token = mPreferences.getString(PREF_SINA_EXPIRES_IN, "0");
	//		try {
	//			Integer.parseInt(token);
	//		} catch (NumberFormatException e) {
	//			token = "0";
	//		}
	//		return token;
	//	}

	//	public void bindSina(String token, String expiresIn) {
	//		SharedPreferences.Editor editor = mPreferences.edit();
	//		editor.remove(PREF_SINA_ACCESS_TOKEN);
	//		editor.remove(PREF_SINA_EXPIRES_IN);
	//		editor.putString(PREF_SINA_ACCESS_TOKEN, token);
	//		editor.putString(PREF_SINA_EXPIRES_IN, expiresIn);
	//		editor.commit();
	//	}
	//
	//	public void unBindSina() {
	//		SharedPreferences.Editor editor = mPreferences.edit();
	//		editor.remove(PREF_SINA_ACCESS_TOKEN);
	//		editor.remove(PREF_SINA_EXPIRES_IN);
	//		editor.commit();
	//		
	//		com.weibo.net.Utility.clearCookies(mContext);
	//	}
	//
	//	public String getRenRenAccessToken() {
	//		return mPreferences.getString(PREF_RENREN_ACCESS_TOKEN, "");
	//
	//	}
	//
	//	public String getRenRenExpiresIn() {
	//		String token = mPreferences.getString(PREF_RENREN_EXPIRES_IN, "0");
	//		try {
	//			Integer.parseInt(token);
	//		} catch (NumberFormatException e) {
	//			token = "0";
	//		}
	//		return token;
	//	}
	//
	//	public void bindRenRen(String token, String expiresIn) {
	//		SharedPreferences.Editor editor = mPreferences.edit();
	//		editor.putString(PREF_RENREN_ACCESS_TOKEN, token);
	//		editor.putString(PREF_RENREN_EXPIRES_IN, expiresIn);
	//		editor.commit();
	//	}
	//
	//	public void unBindRenRen() {
	//		SharedPreferences.Editor editor = mPreferences.edit();
	//		editor.remove(PREF_RENREN_ACCESS_TOKEN);
	//		editor.remove(PREF_RENREN_EXPIRES_IN);
	//		editor.commit();
	//	}
	//
	//	public String getTencentAccessToken() {
	//		return mPreferences.getString(PREF_TENCENT_ACCESS_TOKEN, "");
	//	}
	//
	//	public String getTencentOpenId() {
	//		return mPreferences.getString(PREF_TENCENT_OPEN_ID, "");
	//	}
	//
	//	public String getTencentExpiresIn() {
	//		String token = mPreferences.getString(PREF_TENCENT_EXPIRES_IN, "0");
	//		try {
	//			Integer.parseInt(token);
	//		} catch (NumberFormatException e) {
	//			token = "0";
	//		}
	//		return token;
	//	}
	//
	//	public void bindTencent(String token, String openId, String expiresIn) {
	//		SharedPreferences.Editor editor = mPreferences.edit();
	//		editor.putString(PREF_TENCENT_ACCESS_TOKEN, token);
	//		editor.putString(PREF_TENCENT_OPEN_ID, openId);
	//		editor.putString(PREF_TENCENT_EXPIRES_IN, expiresIn);
	//		editor.commit();
	//	}
	//
	//	public void unBindTencent() {
	//		SharedPreferences.Editor editor = mPreferences.edit();
	//		editor.remove(PREF_TENCENT_ACCESS_TOKEN);
	//		editor.remove(PREF_TENCENT_OPEN_ID);
	//		editor.remove(PREF_TENCENT_EXPIRES_IN);
	//		editor.commit();
	//	}
}
